package com.ttai.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.igexin.sdk.PushManager;
import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerIdItemComponet;
import com.ttai.dagger.module.activity.IdItemPresenterModule;
import com.ttai.golbal.MyApplication;
import com.ttai.presenter.activity.IdItemPresenter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.ui.other.MessageDialog;
import com.ttai.untils.Constant;
import com.ttai.untils.getui.DemoIntentService;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;

public class IdItem extends BaseActivity {

    @Bind(R.id.tv_idfont1)
    TextView tvIdfont1;
    @Bind(R.id.tv_idfont2)
    TextView tvIdfont2;
    @Bind(R.id.tv_idfont3)
    TextView tvIdfont3;
    @Bind(R.id.tv_idfont4)
    TextView tvIdfont4;
    @Bind(R.id.tv_idfont5)
    TextView tvIdfont5;
    @Bind(R.id.tv_idfont6)
    TextView tvIdfont6;
    @Bind(R.id.pb_progressbar_bar)
    ProgressBar pbProgressbarBar;
    @Bind(R.id.tv_idmessagetime)
    TextView tvIdmessagetime;
    @Bind(R.id.tv_idmessageaccount)
    TextView tvIdmessageaccount;
    @Bind(R.id.tv_idmessagenumber)
    TextView tvIdmessagenumber;

    private TextView[] idfont;


    @Inject
    IdItemPresenter idItemPresenter;

    private String acountnumberlogo;
    private String phone;
    private String allmsg;
    private Handler handler;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_idcontext);


        //透明状态栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //透明导航栏
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        TextView barTitle = (TextView)findViewById(R.id.barTitle);
        barTitle.setText("一次性密码");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        ButterKnife.bind(this);
        setComponet();

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);


        idfont = new TextView[]{tvIdfont1, tvIdfont2, tvIdfont3, tvIdfont4, tvIdfont5, tvIdfont6};

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        Intent intent = getIntent();
        acountnumberlogo = intent.getStringExtra("acountnumberlogo");
        phone = intent.getStringExtra("phone");
        String idname = intent.getStringExtra("idname");
        String number = intent.getStringExtra("accountnumber");
        allmsg = "您于   :  " + simpleDateFormat.format(date) + "\n登录   :  " + idname + "\n所获取的验证码是: ";
//        tvIdmessage.setText(allmsg);
        tvIdmessagetime.setText("登录时间: "+simpleDateFormat.format(date));
        tvIdmessageaccount.setText("登录账户: "+idname);
        tvIdmessagenumber.setText("登录账户: "+number);

        String token = Constant.getToken();
        String cid = PushManager.getInstance().getClientid(this);
        Intent intent1 = getIntent();
        String flag = intent1.getStringExtra("acountnumberlogo");
        System.out.println("token:" + token + " cid:" + cid + " flag:" + flag);
        try{
            idItemPresenter.crateOTP(token, cid, flag, "1");
        }catch (Exception e){
//            Toast.makeText(IdItem.this,e.getMessage(),Toast.LENGTH_SHORT).show();
            idItemPresenter.crateOTP(token, cid, flag, "1");
        }

        Thread barThread = new Thread(new MyThread());
        barThread.start();
        handler = new Handler();
    }

    public void showDoalog(String title){
        MessageDialog messageDialog = new MessageDialog(this);
        messageDialog.setTitle("IP异常提醒");
        messageDialog.setMessage(title);
        messageDialog.show();
    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {
        finish();
    }

    @Override
    public void setComponet() {
        DaggerIdItemComponet.builder()
                .idItemPresenterModule(new IdItemPresenterModule(this))
                .build()
                .in(this);
    }

    public void getOTP(String otp) {
        char codeArr[] = otp.toCharArray();
        /**
         * 不同字符添加不同颜色
         */
        for (int i = 0; i < codeArr.length; i++) {
            if (codeArr[i] >= 'a' && codeArr[i] <= 'z') {
                idfont[i].setTextColor(Color.parseColor("#ff0033"));
                idfont[i].setText(String.valueOf(codeArr[i]));
            } else if (codeArr[i] >= 'A' && codeArr[i] <= 'Z') {
                idfont[i].setTextColor(Color.parseColor("#0099FF"));
                idfont[i].setText(String.valueOf(codeArr[i]));
            } else {
                idfont[i].setTextColor(Color.parseColor("#99CC00"));
                idfont[i].setText(String.valueOf(codeArr[i]));
            }
        }
    }

    public static void showWindow(String ip, String appName, String appId, String flag) {
        System.out.println("4546543545");
    }


    class MyThread extends Thread {
        @Override
        public void run() {
            super.run();
            for (int i = 1; i <= 1200; i++) {
                pbProgressbarBar.setProgress(i);
//在子线程中发消息
                handler.sendEmptyMessage(i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            finish();
        }
    }
}
